package com.xiaofeng.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;

import com.xiaofeng.dao.idao.BaseSessionDao;
import com.xiaofeng.dao.idao.Human_ResourceDao;
import com.xiaofeng.pojo.Human_Resource;
import com.xiaofeng.pojo.Page;



@Repository("human_ResourceDao")

public class Human_ResourceDaoImpl extends BaseSessionDao implements Human_ResourceDao{

	
	Session session = null;
	@Override
	public void insert(Human_Resource human_Resource) throws Exception {
		session = getSession();
		Transaction transaction = session.beginTransaction();
		session.save(human_Resource);
		transaction.commit();
		session.close();
	}

	@Override
	public void delete(Human_Resource human_Resource) throws Exception {
		session = getSession();
		Transaction transaction = session.beginTransaction();
		session.delete(human_Resource);
		transaction.commit();
		session.close();
		
	}

	@Override
	public void update(Human_Resource human_Resource) throws Exception {
		session = getSession();
		Transaction transaction = session.beginTransaction();
		session.saveOrUpdate(human_Resource);
		transaction. commit();
		session.close();
		
	}

	@Override
	public Human_Resource findById(int id) throws Exception {
		session = getSession();
		Transaction transaction = session.beginTransaction();
		Human_Resource human_Resource = (Human_Resource) session.createQuery("from Human_Resource where id = ?").setInteger(0, id).uniqueResult();
		transaction.commit();
		session.close();
		return human_Resource;
	}

	

	@SuppressWarnings("unchecked")
	public List<Human_Resource> findall(int num ,int size) throws Exception {
		System.out.println(num);
		System.out.println(size);
		session = getSession();
		Transaction transaction = session.beginTransaction();
		List<Human_Resource> list = session.createQuery("from Human_Resource")
				.setFirstResult((num - 1)*size)
				.setMaxResults(size).list();
		transaction.commit();
		session.close();
		return list;
	}

	@Override
	public Page<Human_Resource> findByPageNo(int pageNo) throws Exception {
		Page<Human_Resource> page = new Page<Human_Resource>();
		page.setTotalNum(getTotalNum());
		page.setPageNo(pageNo);
		page.setList(findall(page.getPageNo(), 3));
		
		return page;
	}
	@Override
	public int getTotalNum(){
		session = getSession();
		Transaction transaction  = session.beginTransaction();
		
		int a = ((Long)session.createQuery("select count(*) from Human_Resource").setCacheable(true).uniqueResult()).intValue();
		
		transaction.commit();
		session.close();
		
		return a;
		
		
	}
	
}
